package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.core.common.node.JsonResult;
import cn.stylefeng.guns.modular.api.model.DepartmentDetailsPageDTO;
import cn.stylefeng.guns.modular.api.model.DetailsPageDTO;
import cn.stylefeng.guns.modular.api.model.ExpertDTO;
import cn.stylefeng.guns.modular.api.model.ExpertDetailDTO;
import cn.stylefeng.guns.modular.hp.constant.ArticleEnum;
import cn.stylefeng.guns.modular.hp.constant.ListTypeEnum;
import cn.stylefeng.guns.modular.hp.constant.MenuJumpTypeEnum;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.ExpertInfo;
import cn.stylefeng.guns.modular.hp.model.PageContentInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IExpertInfoService;
import cn.stylefeng.guns.modular.hp.service.IPageContentInfoService;
import cn.stylefeng.guns.modular.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/detailsPage")
@Api(tags = "1.0", description = "市中医院门户详情页", value = "门户详情页API")
public class DetailsPageApiController {

	private final static Logger log = LoggerFactory.getLogger(DetailsPageApiController.class);
	
	@Autowired
    private IPageContentInfoService pageContentInfoService;

	@Autowired
	private IArticleInfoService articleInfoService;

	@Autowired
	private IExpertInfoService expertInfoService;

	@Autowired
	private IUserService userService;
	
	@GetMapping("/getPage")
    @ResponseBody
	@ApiOperation(value = "根据ID获取页面内容")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "页面ID", dataType = "String", paramType = "query")
	})
	public JsonResult<DetailsPageDTO> getPage(@RequestParam String id, String modelCode) {
		JsonResult jr = new JsonResult();
		PageContentInfo page = this.pageContentInfoService.selectById(id);
		ArticleInfo articleInfo = this.articleInfoService.getArticleInfoByPageInfoId(id);
		ExpertInfo expertInfo = expertInfoService.getExpertInfoByPageInfoId(id);
		if(page != null) {
			if(articleInfo != null){
				if(ArticleEnum.DEPARTMENT.equals(articleInfo.getModular())){
					DepartmentDetailsPageDTO departmentDetailsPageDTO = new DepartmentDetailsPageDTO(articleInfo.getTitle(),this.userService.getAccountNameById(page.getCreater()),page);
					departmentDetailsPageDTO.setModelCode(articleInfo.getModular());
					departmentDetailsPageDTO.setModelListUrl(getModelListUrl(articleInfo.getModular()));

					List<ExpertInfo> infoList = this.expertInfoService.departmentExpertList(articleInfo.getId());

					List<ExpertDTO> expertDTOList = new ArrayList<ExpertDTO>();

					if(infoList != null && infoList.size() > 0) {
						for (int i = 0; i < infoList.size(); i++) {
							ExpertInfo info = infoList.get(i);
							expertDTOList.add(new ExpertDTO(info.getName(), info.getName(), info.getRank(), info.getPicUrl(), info.getPageContentId(), info.getPri(), MenuJumpTypeEnum.DETAILS));
						}
					}
					departmentDetailsPageDTO.setExpertList(expertDTOList);

					jr.setResultCode(0);
					jr.setResultMsg("请求成功！");
					jr.setResultData(departmentDetailsPageDTO);
				}else{
					DetailsPageDTO detailsPageDTO = new DetailsPageDTO(articleInfo.getTitle(),this.userService.getAccountNameById(page.getCreater()),page);
					detailsPageDTO.setModelCode(articleInfo.getModular());
					detailsPageDTO.setModelListUrl(getModelListUrl(articleInfo.getModular()));

					jr.setResultCode(0);
					jr.setResultMsg("请求成功！");
					jr.setResultData(detailsPageDTO);
				}
			}else if(expertInfo != null){
				ExpertDetailDTO expertDetailDTO = new ExpertDetailDTO(expertInfo.getName(),expertInfo.getName(),expertInfo.getRank(),expertInfo.getPicUrl(),page.getContent());
				expertDetailDTO.setAuthor(this.userService.getAccountNameById(page.getCreater()));
				expertDetailDTO.setCreateTime(page.getCreatedTime());
				expertDetailDTO.setModelCode(ListTypeEnum.EXPERT);
				expertDetailDTO.setModelListUrl(getModelListUrl(ListTypeEnum.EXPERT));

				jr.setResultCode(0);
				jr.setResultMsg("请求成功！");
				jr.setResultData(expertDetailDTO);
			}else{
				DetailsPageDTO detailsPageDTO = new DetailsPageDTO(page.getTitle(),this.userService.getAccountNameById(page.getCreater()),page);
				detailsPageDTO.setModelCode(modelCode);

				jr.setResultCode(0);
				jr.setResultMsg("请求成功！");
				jr.setResultData(detailsPageDTO);
			}
		}else{
			jr.setResultCode(1);
			jr.setResultMsg("请求失败，不存在的内容！");
			jr.setResultData(null);
		}
		return jr;
	}

	/**
	 * 获取模块列表页地址
	 * @param modularType
	 * @return
	 */
	private String getModelListUrl(String modularType){
		if(ListTypeEnum.NEWS.equals(modularType)){
			return ListTypeEnum.NEWS_URL;
		}else if(ListTypeEnum.NOTICE.equals(modularType)){
			return ListTypeEnum.NOTICE_URL;
		}else if(ListTypeEnum.EXPERT.equals(modularType)){
			return ListTypeEnum.EXPERT_URL;
		}else{
			return ListTypeEnum.DEFAULT_URL;
		}
	}
}
